import Vue from "vue";
import Vuex from "vuex";
import vuexPersist from "vuex-persist";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    todolist: [
      {
        title: "任务1",
        state: 0,
      },
      {
        title: "任务2",
        state: 1,
      },
      {
        title: "任务3",
        state: 2,
      },
    ],
    //
  },
  mutations: {
    add(state, obj) {
      state.todolist.push(obj);
      
    },

    change(state, i) {
      state.todolist.forEach((item, index) => {
        if (index == i) {
          if (item.state == 0) {
            item.state = 1;
            item.time = new Date().toLocaleString();
          } else {
            item.state = 0;
          }
        }
      });
    },
    cancel(state, i) {
      state.todolist.forEach((item, index) => {
        if (index == i) {
          if (item.state == 0) {
            item.state = 2;
          } else {
            item.state = 0;
          }
        }
      });
    },
  },
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
});